<?php

namespace cf\api\tmp;

require_once dirname(__FILE__).'/admin.php';

function add()
{
	$id = \cf\api\admin\doAction('tmp_file_add',array('f'=>''));
	global $returnHTML;	
	$returnHTML = true;
	return json_encode(array($id, \cf\query2var('SELECT f FROM cf_tmp_files WHERE id=:id',array('id'=>$id))));
}

function save($id,$target)
{
	if (!is_array($id)) {
		$id = array($id);
	}
	$q = \cf\createQuery('SELECT f FROM cf_tmp_files WHERE id=:id');
	$qDel = \cf\createQuery('DELETE FROM cf_tmp_files WHERE id=:id');
	foreach($id as $i) {
		$f = pathinfo(\cf\query2var($q,array('id'=>$i)));
		if ($f['basename']) {
			@rename($_SERVER['DOCUMENT_ROOT'].'/'.$f['dirname'].'/'.$f['basename'], $_SERVER['DOCUMENT_ROOT']."/$target/".$f['basename']);
		}
		$qDel->setParam('id',$i)->execute();
	}
}

function del($id)
{
	if (!is_array($id)) {
		$id = array($id);
	}
	array_walk($id,function(&$v,$k) { $v = array('id'=>$v); });
	\cf\api\admin\doDelete('tmp_file_del', $id);
}


?>